<template>
	<view class="account">
		<view class="user-avatar">
			<image @click="upload()" :src="userInfo.avatar ?  userInfo.avatar : require('../../../static/images/avatar.png')" mode="aspectFill">
			</image>
			<view class="user-name">
				<view class="user-n" @click="goPage('/pages/users/account-edit/account-edit?action=username')">
					<text>{{userInfo.username}}</text>
					<text class="iconfont icon-edit"></text>
				</view>
				<text class="user-id">UID:{{userInfo.id}}</text>
			</view>
		</view>
		<view class="lists">
			<view class="list" @click="copy(userInfo.invitation_code)">
				<view class="list-left">
					<text>邀请码</text>
				</view>
				<view class="list-right">
					<text>{{userInfo.invitation_code}}</text>
					<text class="iconfont icon-dayu"></text>
				</view>
			</view>
			<view class="list">
				<view class="list-left">
					<text>手机号</text>
				</view>
				<view class="list-right">
					<text>{{userInfo.phone}}</text>
					<text class="iconfont icon-dayu"></text>
				</view>
			</view>
			<view class="list">
				<view class="list-left">
					<text>悬赏主</text>
				</view>
				<view class="list-right">
					<text v-if="!userInfo.reward_lord" @click="goPage('/pages/public/not-open-reward/not-open-reward')">去开通</text>
					<text v-else>{{formatDate(userInfo.reward_end_time*1000)}}</text>
					<text class="iconfont icon-dayu"></text>
				</view>
			</view>
			<view class="list">
				<view class="list-left">
					<text>注册时间</text>
				</view>
				<view class="list-right">
					<text>{{formatDate(userInfo.create_time*1000)}}</text>
					<text class="iconfont icon-dayu"></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapActions} from "vuex";
	import {formatDate} from '@/utils/util.js'
	export default{
		name:"account",
		computed:{
			...mapState(["isLogin","userInfo"]),
		},
		methods:{
			...mapActions(["setUserInfo_action"]),
			formatDate(val){
				return formatDate(val)
			},
			goPage(url){
				uni.navigateTo({
					url:url
				})
			},
			//头像上传
			upload() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						//图片上传
						const uploadTask = uni.uploadFile({
							url:this.$baseURL+'/api/upload/uploadAvatar',
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								'Authorization': uni.getStorageSync('oauth_token')
							},
							success: (uploadFileRes) => {
								let res = JSON.parse(uploadFileRes.data);
								if(res.code === 1){
									this.setUserInfo_action()
								}
							}
						});
						uploadTask.onProgressUpdate((res) => {
						    if (res.progress > 5000) {
								this.$msg("上传失败")
						        uploadTask.abort();
						    }
						});
					}
				});
			},
			//copy
			copy(code){
				uni.setClipboardData({
				    data: code
				});
			}
		}
	}
</script>

<style scoped lang="less" src="./account.less">
</style>
